<?php
namespace app\api\controller\wsgoods;
class Goodsevaluate extends Base{

    public function lists(){
        $d = $this->post;
        $rule = [
            'goods_id'=>'require',
            'page'=>'require|integer',
            'limit'=>'require|integer',
        ];
        $msg = [
            'goods_id.require' => '商品id不能为空',
            'page.require' => 'page不能为空',
            'limit.require' => 'limit不能为空',
        ];
        check($rule,$msg,$d);

        $map = [];
        $map[] = ["and","a.goods_id","=",$d['goods_id']];
        if($d['grade']!='') $map[] = ["and","a.grade","in",explode(",",$d['grade'])];
        $w = where($map);
        $sql = "
            select
                a.*,b.pic as user_pic,b.username,c.title as goods_name,c.goods_price_title
            from
                goods_evaluate a
            left join
                user b on a.user_id = b.id
            left join
                order_goods c on c.id = a.order_goods_id
        $w[0] order by id desc " . limit($d['page'], $d['limit']);
        $rs = $this->db->query($sql, $w[1], '', true);
        foreach ($rs[0] as $k => $v) {
            $rs[0][$k]['create_time_name'] = com_time_format($v["create_time"]);
        }
        //评论数量
        $ecount = m("GoodsEvaluate","c")->getCount($d['goods_id']);
        suc(['list'=>$rs[0],'ecount'=>$ecount],$rs[1]);
    }

    //保存订单商品评价
    public function add(){
        $this->checkLogin();
        $d = $this->post();
        $rule = [
            'order_goods_id'=>'require',
            'content'=>'require',
        ];
        $msg = [
            'order_goods_id.require' => '订单ID不能为空',
            'content.require' => '请输入您的评价',
        ];
        check($rule,$msg,$d);

        $order = $this->db->query('select * from order_goods where id=? and user_id=?',[$d['order_goods_id'],$this->userinfo['id']],1);
        if(!$order){
            err("订单不存在!");
        }
        if($order['fahuo_status'] == 0){
            err("订单还未发货!");
        }
        if($order['sh_status'] == 0){
            err("订单还未收货!");
        }
        if($order['pj_status'] == 1){
            err("该订单已评价!");
        }
        $data = [
            'user_id' => $this->userinfo["id"],
            'goods_id' => $order["goods_id"],
            'goods_price_id' => $order["goods_price_id"],
            'order_goods_id' => $d["order_goods_id"],
            'grade' => $d["grade"],
            'pics' => $d["pics"],
            'content' => $d["content"],
            'anonymous' => (int)$d["anonymous"],
            'create_time' => time(),
        ];

        $this->db->insert('goods_evaluate',$data);
        $this->db->update('order_goods',['id'=>$d['order_goods_id'],'pj_status'=>1]);
        //$this->db->update('order_goods_no',['order_no'=>$d['order_no'],'status'=>5]);
        $this->db->query("update order_goods_no set status=5,pj_time=? where order_no=?",[time(),$order['order_no']]);
        suc('评价成功!');
    }


    //我的评价
    public function myEvaluate(){
        $this->checkLogin();
        $d = $this->post;
        $rule = [
            'page'=>'require',
            'limit'=>'require',
        ];
        $msg = [
            'page.require' => 'page不能为空',
            'limit.require' => 'limit不能为空',
        ];
        check($rule,$msg,$d);

        $map = [];
        $map[] = ["and","a.user_id","=",$this->userinfo["id"]];
        $w = where($map);
        $rs = $this->db->query("
            select
                a.*,b.username,b.pic as user_pic,c.pic as goods_pic,c.title as goods_name,c.goods_price_title,c.goods_price_price
            from
                goods_evaluate a
            left join user b on a.user_id = b.id
            left join order_goods c on c.id = a.order_goods_id
            {$w[0]} order by id desc
        ".limit($d['page'],$d['limit']),$w[1],'',true);
        if($rs){
            foreach ($rs[0] as $k => $v) {
                $rs[0][$k]['create_time_name'] = com_time_format($v["create_time"]);
            }
            suc($rs[0],$rs[1]);
        }
        suc();
    }

}
?>
